<?php

namespace app\company\model;


use Exception;
use think\Model;

/**
 * @property  int $id
 * @property  int $company_order_id 订单ID
 * @property  int $goods_id 商品ID
 * @property  int $quantity 数量
 * @property  float $purchasing_price 进价
 * @property  float $amount
 * @property  int $status
 * @property  int $goods_rule_id
 * @property  int $company_id
 * @property  int $orig_quantity 订单签收前数量
 * @property  float $orig_amount 订单签收前金额
 * @property  int $production_time 生产时间
 * @property  int $expire_time  过期时间
 * @property  int $supplier_id 经销商ID
 * @property  int $goods_cate_id 商品分类ID
 */
class CompanyOrderDetail extends Base
{

    public static function onBeforeInsert(CompanyOrderDetail $model): void
    {
        $model->orig_quantity = $model->quantity;
        $model->orig_amount   = $model->amount;
    }

    /**
     * 更新前
     * @param CompanyOrderDetail $model
     * @return void
     * @throws Exception
     */
    public static function onBeforeUpdate(CompanyOrderDetail $model): void
    {
        //更新数量
        if ($quantity = request()->param('quantity', 0)) {
            if ($model->companyOrder->order_status == 5) exception('订单已完成，无法修改');
            $model->amount = $model->purchasing_price * $quantity;
            //当没有审核之前修改时,同时修改原订单数量和原订单金额
            if ($model->companyOrder->order_status < 2) {
                $model->orig_quantity = $model->quantity;
                $model->orig_amount   = $model->amount;
            }
        }

        //更新库位
        if ($warehouse_storages = request()->param('warehouse_storages', 0)) {
            if ($model->companyOrder->order_status != 3) exception('订单未签收,无法分配库位');
            if ($model->companyOrderStorages()->count()) $model->companyOrderStorages->delete();
            $count = array_sum(array_column($warehouse_storages, 'quantity'));
            if ($count != $model->quantity) exception('分配数量与订单数量不一致,无法分配库位,签收数量' . $model->quantity . ',分配数量' . $count);
            $base = [
                'company_id'              => $model->company_id,
                'supplier_id'             => $model->supplier_id,
                'company_order_id'        => $model->company_order_id,
                'company_order_detail_id' => $model->id,
                'goods_id'                => $model->goods_id,
                'goods_rule_id'           => $model->goods_rule_id,
                'goods_cate_id'           => $model->goods_cate_id,
                'status'                  => 2,//此时订单还未上架,所以是禁用状态,未上redis库存,不能消费
                'production_time'         => $model->production_time,//生产时间
                'expire_time'             => $model->expire_time,//过期时间
            ];
            $arr  = [];
            foreach ($warehouse_storages as $v) {
                if (!$storageInfo = WarehouseStorage::find($v['warehouse_storage_id'])) exception('库位不存在');
                $base['quantity']               = $v['quantity'];
                $base['orig_quantity']          = $v['quantity'];
                $base['warehouse_storage_id']   = $v['warehouse_storage_id'];
                $base['warehouse_id']           = $storageInfo->warehouse_id;
                $base['warehouse_area_id']      = $storageInfo->warehouse_area_id;
                $base['warehouse_storage_name'] = $storageInfo['name'];//name 是类初始化属性,不能用->获取
                $arr[]                          = $base;
            }
            //添加
            $model->companyOrderStorages()->saveAll($arr);
        }

        //更新过期时间
        if ($production_time = request()->param('production_time', 0)) {
            //获取保质期
            $shelf_life      = $model->goods()->value('shelf_life');
            $production_time = strtotime($production_time);
            //生产日期
            $model->production_time = $production_time;
            $model->expire_time     = strtotime('+' . $shelf_life . ' day', $production_time);
            if ($model->companyOrderStorages()->count()) {
                $model->companyOrderStorages->update([
                    'expire_time'     => $model->expire_time,
                    'production_time' => $production_time
                ]);
            }
        }
    }


}